﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace SampleKeyValueDemo.Relationships
{
    public class CacheRelationship<T> : IKeyValueDataStore<T>
    {
        private IKeyValueDataStore<T> cache;
        private IKeyValueDataStore<T> source;
        public CacheRelationship(IKeyValueDataStore<T> cache,
            IKeyValueDataStore<T> source)
        {
            this.cache = cache;
            this.source = source;
        }

        public void Set(string key, T val)
        {
            this.cache.Set(key, val);
            this.source.Set(key, val);
        }

        public T Get(string key)
        {
            T d = this.cache.Get(key);
            if (d != null) { return d; }
            d = this.source.Get(key);
            if (d != null)
            {
                this.cache.Set(key, d);
            }
            return d;
        }

        public void Remove(string key)
        {
            this.cache.Remove(key);
            this.source.Remove(key);
        }
    }
}